package com.livery.login;

import javax.swing.JDialog;
import java.awt.Dialog.ModalityType;
import java.awt.GridLayout;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import com.livery.login.validators.LoginValidator;
import com.livery.login.validators.Validator;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LoginView extends JDialog implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8516623401811721551L;
	private JTextField textField;
	private JPasswordField passwordField;
	private JButton btnLogin;
	private JButton btnExit;
	
	public LoginView(){
		initComponents();
		//setSize(265, 143);
		setLocationRelativeTo(null);
		
	}

	private void initComponents() {
		setModalityType(ModalityType.APPLICATION_MODAL);
		passwordField = new javax.swing.JPasswordField();
		textField = new javax.swing.JTextField();
		JPanel jPanel1 = new javax.swing.JPanel();
		btnExit = new javax.swing.JButton();
		btnLogin = new javax.swing.JButton();
		btnLogin.addActionListener(this);
		btnExit.addActionListener(this);

		//setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

		btnExit.setText("Zamknij");
		jPanel1.add(btnExit);

		btnLogin.setText("Zaloguj");
		jPanel1.add(btnLogin);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														jPanel1,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														241, Short.MAX_VALUE)
												.addComponent(textField)
												.addComponent(passwordField))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addGap(27, 27, 27)
								.addComponent(textField,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(18, 18, 18)
								.addComponent(passwordField,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addComponent(jPanel1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(5, 5, 5)));

		pack();

	}
/*
	public LoginView() {
		
		setModalityType(ModalityType.APPLICATION_MODAL);
		// setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);

		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout(0, 0));

		textField = new JTextField();
		panel.add(textField, BorderLayout.CENTER);
		textField.setColumns(10);

		JPanel panel_1 = new JPanel();
		panel_1.setLayout(new BorderLayout(0, 0));

		passwordField = new JPasswordField();
		panel_1.add(passwordField, BorderLayout.CENTER);

		JPanel panel_2 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);

		btnExit = new JButton("Wyjdź");
		panel_2.add(btnExit);

		btnLogin = new JButton("Zaloguj");
		btnLogin.addActionListener(this);
		panel_2.add(btnLogin);
		getContentPane().setLayout(new GridLayout(0, 1, 0, 0));
		getContentPane().add(panel);
		getContentPane().add(panel_1);
		getContentPane().add(panel_2);
	}
*/
	@Override
	public void actionPerformed(ActionEvent ev) {
		if (ev.getSource() == btnLogin) {
			Validator v = new LoginValidator(textField.getText(),
					String.valueOf(passwordField.getPassword()));
			if (v.validate()) {
				this.dispose();
			} else {
				try {
					JOptionPane.showMessageDialog(null,
							"Nie prawidłowy login lub hasło", "Bład logowania",
							JOptionPane.ERROR_MESSAGE);
				} catch (Exception e) {

				}
			}
		}
	}

}
